package com.melesta.engine;

import android.annotation.SuppressLint;
import android.app.Application;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.AssetManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.provider.Settings.Secure;
import android.telephony.TelephonyManager;
import android.text.ClipboardManager;
import android.util.TypedValue;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.UUID;

public class EngineApp extends Application
{
  protected static final String PREFS_DEVICE_ID = "device_id";
  protected static final String PREFS_DEVICE_ID_FILE = "device_id.xml";
  private static EngineApp instance;
  private static String[] mTD1PackageNames = { "com.melesta.toydefense", "com.melesta.amazon.toydefense", "com.melesta.amazon.toydefensefree" };
  private static String[] mTD2PackageNames = { "com.melesta.toydefense2", "com.melesta.toydefense2free", "com.melesta.amazon.toydefense2", "com.melesta.amazon.toydefense2free" };
  private static String mUuid;
  private String mLanguage;

  public static boolean checkInstalledToyDefense1()
  {
    EngineApp localEngineApp = getApp();
    boolean bool = false;
    if (localEngineApp == null)
      return bool;
    PackageManager localPackageManager = localEngineApp.getPackageManager();
    bool = false;
    int i = -1 + mTD1PackageNames.length;
    while ((i >= 0) && (!bool))
      try
      {
        localPackageManager.getPackageInfo(mTD1PackageNames[i], 0);
        bool = true;
        label47: i--;
      }
      catch (PackageManager.NameNotFoundException localNameNotFoundException)
      {
        break label47;
      }
  }

  public static boolean checkInstalledToyDefense2()
  {
    EngineApp localEngineApp = getApp();
    boolean bool = false;
    if (localEngineApp == null)
      return bool;
    PackageManager localPackageManager = localEngineApp.getPackageManager();
    bool = false;
    int i = -1 + mTD2PackageNames.length;
    while ((i >= 0) && (!bool))
      try
      {
        localPackageManager.getPackageInfo(mTD2PackageNames[i], 0);
        bool = true;
        label47: i--;
      }
      catch (PackageManager.NameNotFoundException localNameNotFoundException)
      {
        break label47;
      }
  }

  public static void copyTextToClipboard(String paramString)
  {
    try
    {
      EngineActivity localEngineActivity = EngineActivity.getInstance();
      if (localEngineActivity == null)
        return;
      localEngineActivity.runOnUiThread(new Runnable()
      {
        public void run()
        {
          ((ClipboardManager)EngineApp.getApp().getSystemService("clipboard")).setText(this.val$str);
        }
      });
      return;
    }
    catch (Exception localException)
    {
      Error.processException("copyTextToClipboard", localException);
    }
  }

  public static void fileDelete(String paramString)
  {
    DatabaseHelper.getHelper(getApp()).fileDelete(paramString);
  }

  public static void fileSave(String paramString, byte[] paramArrayOfByte)
  {
    DatabaseHelper.getHelper(getApp()).saveFile(paramString, paramArrayOfByte);
  }

  public static String getApkFilePath()
  {
    try
    {
      EngineApp localEngineApp = getApp();
      PackageManager localPackageManager = localEngineApp.getPackageManager();
      String str1 = localEngineApp.getPackageName();
      Iterator localIterator = localPackageManager.getInstalledApplications(0).iterator();
      while (localIterator.hasNext())
      {
        ApplicationInfo localApplicationInfo = (ApplicationInfo)localIterator.next();
        if (localApplicationInfo.packageName.equals(str1))
        {
          String str2 = localApplicationInfo.sourceDir;
          return str2;
        }
      }
    }
    catch (Exception localException)
    {
      Error.processException(localException);
    }
    return null;
  }

  public static EngineApp getApp()
  {
    return instance;
  }

  public static String getAppId()
  {
    EngineApp localEngineApp = getApp();
    if (localEngineApp == null)
      return "";
    return localEngineApp.getPackageName();
  }

  public static String getAppVersion()
  {
    if (instance == null)
      return "";
    try
    {
      String str = instance.getPackageManager().getPackageInfo(instance.getPackageName(), 0).versionName;
      return str;
    }
    catch (PackageManager.NameNotFoundException localNameNotFoundException)
    {
      Log.e("engine", "Package name not found", localNameNotFoundException);
    }
    return "";
  }

  public static AssetManager getAssetManager()
  {
    try
    {
      EngineApp localEngineApp = getApp();
      if (localEngineApp != null)
      {
        AssetManager localAssetManager = localEngineApp.getAssets();
        return localAssetManager;
      }
    }
    catch (Exception localException)
    {
      Error.processException(localException);
    }
    return null;
  }

  @SuppressLint({"DefaultLocale"})
  public static String getConfigString(String paramString)
  {
    String str1;
    try
    {
      EngineApp localEngineApp = getApp();
      if (localEngineApp == null)
        return "";
      if (paramString.equalsIgnoreCase("RateMeUrl"))
      {
        String str2 = localEngineApp.getPackageName();
        if (str2.toLowerCase().contains("amazon"))
          return String.format("http://www.amazon.com/gp/mas/dl/android?p=%s", new Object[] { str2 });
        return String.format("market://details?id=%s", new Object[] { str2 });
      }
      Resources localResources = localEngineApp.getResources();
      if (paramString.equalsIgnoreCase("TweetUrl"))
        return localResources.getString(R.string.TweetUrl);
      if (paramString.equalsIgnoreCase("TwitterHashTag"))
        return localResources.getString(R.string.TwitterHashTag);
      if (paramString.equalsIgnoreCase("ToyDefense2App"))
        return localResources.getString(R.string.ToyDefense2App);
      TypedValue localTypedValue = new TypedValue();
      localResources.getValue(paramString, localTypedValue, true);
      str1 = localTypedValue.string.toString();
      if (str1 == null)
        return "";
    }
    catch (Exception localException)
    {
      Log.d("engine", "Can't get config value for " + paramString);
      str1 = "";
    }
    return str1;
  }

  public static String getCurrentLanguage()
  {
    if (getApp() != null)
      return getApp().mLanguage;
    return null;
  }

  public static String getDeviceId()
  {
    if (mUuid != null)
      return mUuid;
    EngineApp localEngineApp = getApp();
    if (localEngineApp == null)
      return "";
    SharedPreferences localSharedPreferences = localEngineApp.getSharedPreferences("device_id.xml", 0);
    String str1 = localSharedPreferences.getString("device_id", null);
    if (str1 != null)
    {
      mUuid = str1;
      return mUuid;
    }
    String str2 = Settings.Secure.getString(localEngineApp.getContentResolver(), "android_id");
    while (true)
    {
      try
      {
        if (!"9774d56d682e549c".equals(str2))
        {
          mUuid = UUID.nameUUIDFromBytes(str2.getBytes("utf8")).toString();
          localSharedPreferences.edit().putString("device_id", mUuid).commit();
          break;
        }
        String str3 = ((TelephonyManager)localEngineApp.getSystemService("phone")).getDeviceId();
        if (str3 != null)
        {
          localObject = UUID.nameUUIDFromBytes(str3.getBytes("utf8"));
          mUuid = ((UUID)localObject).toString();
          continue;
        }
      }
      catch (UnsupportedEncodingException localUnsupportedEncodingException)
      {
        throw new RuntimeException(localUnsupportedEncodingException);
      }
      UUID localUUID = UUID.randomUUID();
      Object localObject = localUUID;
    }
  }

  public static byte[] getFile(String paramString)
  {
    return DatabaseHelper.getHelper(getApp()).getFile(paramString);
  }

  public static List<String> getFileList()
  {
    return DatabaseHelper.getHelper(getApp()).getFilesList(true);
  }

  public static boolean isFileExist(String paramString)
  {
    return DatabaseHelper.getHelper(getApp()).isExists(paramString);
  }

  public static boolean isNetworkAvailable()
  {
    if (getApp() == null);
    NetworkInfo localNetworkInfo;
    do
    {
      return false;
      localNetworkInfo = ((ConnectivityManager)getApp().getSystemService("connectivity")).getActiveNetworkInfo();
    }
    while ((localNetworkInfo == null) || (!localNetworkInfo.isConnectedOrConnecting()));
    return true;
  }

  public void onCreate()
  {
    instance = this;
    super.onCreate();
    Log.d("java", "app - onCreate");
    try
    {
      Resources localResources = getResources();
      Locale localLocale = localResources.getConfiguration().locale;
      this.mLanguage = localLocale.getLanguage();
      Log.d("engine", "Language:" + this.mLanguage + "  " + localLocale.getISO3Language());
      String str1 = localLocale.getCountry();
      Log.d("engine", "Country:" + localLocale.getISO3Country() + "  " + str1);
      if ((this.mLanguage.equalsIgnoreCase("zh")) && (str1.equalsIgnoreCase("cn")))
        this.mLanguage = "zh-hans";
      for (String str2 : localResources.getStringArray(R.array.nativeLibs))
      {
        Log.d("java", "load lib " + str2);
        System.loadLibrary(str2);
      }
      DatabaseHelper.getHelper(this);
      NativeEngineCallbacks.create();
      Log.d("java", "app - onCreate-Done!");
      return;
    }
    catch (Exception localException)
    {
      while (true)
        Error.processException("app-onCreate", localException);
    }
  }

  public void onTerminate()
  {
    NativeEngineCallbacks.destroy();
    super.onTerminate();
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.melesta.engine.EngineApp
 * JD-Core Version:    0.6.2
 */